package com.sxt.sys.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sxt.sys.utils.WebUtils;
import com.sxt.sys.SYSConstast.SYSConstast;
import com.sxt.sys.domain.Dept;
import com.sxt.sys.domain.Role;
import com.sxt.sys.domain.User;
import com.sxt.sys.service.DeptService;
import com.sxt.sys.service.RoleService;
import com.sxt.sys.service.UserService;
import com.sxt.sys.utils.DataGridView;
import com.sxt.sys.utils.Md5Utils;
import com.sxt.sys.utils.PinYinUtils;
import com.sxt.sys.utils.ResultObj;
import com.sxt.sys.vo.RoleVo;
import com.sxt.sys.vo.UserVo;

@RestController
@RequestMapping("user")
public class UserController {
	
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private RoleService roleService;
	//查询
	@RequestMapping("loadAllUser")
	public DataGridView loadAllUser(UserVo userVo) {
		return userService.queryAllUser(userVo);
	}
	
	@RequestMapping("deleteUser")
	public ResultObj deleteUser(UserVo userVo) {
		Integer code=1;
		String msg="";
		try {
			Integer info = userService.deleteUser(userVo.getId());
			code = info;
			msg = info > 0 ?SYSConstast.DELETE_LOGININFOR_TRUE:SYSConstast.DELETE_LOGININFOR_FALSE;
		} catch (Exception e) {
			code = 0;
			msg = SYSConstast.DELETE_LOGININFOR_FALSE;
		}
		return new ResultObj(code , msg);
	}
	
	@RequestMapping("addUser")
	public ResultObj addNotice(UserVo userVo) {
		Integer code=1;
		String msg="";
		try {
			
			
			Integer info = userService.addUser(userVo);
			code = info;
			msg = info > 0 ?SYSConstast.ADD_LOGININFOR_TRUE:SYSConstast.ADD_LOGININFOR_FALSE;
		} catch (Exception e) {
			code = 0;
			msg = SYSConstast.ADD_LOGININFOR_FALSE;
		}
		return new ResultObj(code , msg);
	}
	
	
	@RequestMapping("updateUser")
	public ResultObj updateNotice(UserVo userVo) {
		Integer code=1;
		String msg="";
		try {
			Integer info = userService.updateUser(userVo);
			code = info;
			msg = info > 0 ?SYSConstast.UPDATE_LOGININFOR_TRUE:SYSConstast.UPDATE_LOGININFOR_FALSE;
		} catch (Exception e) {
			code = 0;
			msg = SYSConstast.UPDATE_LOGININFOR_FALSE;
		}
		return new ResultObj(code , msg);
	}
	
	
	@RequestMapping("loadUserBydeptid")
	public List<User> loadUserBydeptid(UserVo userVo){
		List<User> user = userService.queryUserBydeptid(userVo);
		
		return user;
	}
	
	@RequestMapping("nameChangdPinYin")
	public Map<String, Object> nameChangdPinYin(UserVo userVo){
		Map<String, Object> map = new HashMap<>();
		map.put("value", PinYinUtils.toPinyin(userVo.getName()));
		return map;
	}

	
	
	//取出排序最大值
	@RequestMapping("loadMaxOrderNumber")
	public Map<String, Integer> loadMaxOrderNumber(){
		Map<String, Integer> map = new HashMap<>();
		Integer value = userService.queryMaxOrderNumber();
		
		map.put("value", value);
		
		return map;
	}
	
	@RequestMapping("updatePwd")
	public ResultObj updatePwd(UserVo userVo) {
		Integer code=1;
		String msg="";
		try {
			Integer info = userService.updateUserPwd(userVo);
			code = info;
			msg = info > 0 ?SYSConstast.UPDATE_USERPWD_TRUE:SYSConstast.UPDATE_LOGININFOR_FALSE;
		} catch (Exception e) {
			code = 0;
			msg = SYSConstast.UPDATE_LOGININFOR_FALSE;
		}
		return new ResultObj(code , msg);
	}
	
	@RequestMapping("loadUserByUserId")
	public User loadUserByUserId(UserVo userVo) {
		return userService.queryUserByMgr(userVo.getId());
	}
	
	@RequestMapping("loadAllRoleByUserId")
	public DataGridView loadAllRoleByUserId(UserVo userVo) {
		RoleVo role = new RoleVo();
		role.setAvailable(SYSConstast.MENU_AVAILABLE_TRUE);
		List<Role> forlist = roleService.queryAllRoleForlist(role);
		List<Role> list = roleService.queryAllRoleByUserId(userVo.getId());
		List<Map<String, Object>> data = new ArrayList<>();
		
		for (Role r1 : forlist) {
			Boolean checked = false;
			for (Role r2 : list) {
				
				if(r2.getId()==r1.getId()) {
					checked = true;
					break;
				}
			}
			Map<String, Object> map = new HashMap<>();
			map.put("id", r1.getId());
			map.put("name", r1.getName());
			map.put("remark", r1.getRemark());
			if(checked) {
				map.put("LAY_CHECKED", checked);
			}
			data.add(map);
		}
		return new DataGridView(data.size()+0L,data);
	}
	@RequestMapping("saveUserRole")
	public ResultObj saveUserRole(UserVo userVo) {
		Integer code=1;
		String msg="";
		try {
			Integer info = userService.saveUserRole(userVo);
			code = info;
			msg = info > 0 ?SYSConstast.UPDATE_USERPWD_TRUE:SYSConstast.UPDATE_LOGININFOR_FALSE;
		} catch (Exception e) {
			code = 0;
			msg = SYSConstast.UPDATE_LOGININFOR_FALSE;
		}
		return new ResultObj(code , msg);
	} 
	
}
